const {verifyToken} = require('../utils/tokenUtils')
const {DATAERROR} = require('../utils/constants')
module.exports=async (req,res,next)=>{
    const token = req.headers.token//token自定义在请求头中
    //预检请求判断
    if(req.method === 'OPTIONS'){
        next()
        return
    }
    try {
        let data =await verifyToken(token)
        // console.log(data);
        
        req.userId=data.userId
        // console.log(req.userId);
        
        next()
    } catch (error) {
        res.json({
            "code":DATAERROR,
            "message":error.message
        })
    }
}